package logic.ships.mothership;

import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.TriMesh;

import logic.slots.AbstractSlotCreator;

public class SpawnSlotCreator extends AbstractSlotCreator {
	
	private final String SPAWN = "spawn";
	private final String DRONE = "drone";
	private final String CARRIER = "carrier";
	private final String HUNTER = "hunter";
	
	private SpawnSlot droneSpawn, carrierSpawn, hunterSpawn;
	private float orientationAngle;
	private MotherShip ms;
	
	public SpawnSlotCreator(MotherShip motherShip) {
		super();
		ms = motherShip;
		
		orientationAngle = motherShip.getTeam().getMSOrientationAngle();
		
		init(motherShip.getLowQualityNode());
		assert(droneSpawn != null);
		assert(carrierSpawn != null);
		assert(hunterSpawn != null);
	}
	
	private void init(Node parent) {
		for(Spatial child : parent.getChildren()) {
			if(child instanceof Node) init((Node)child);
			else if(child instanceof TriMesh) {
				TriMesh mesh = (TriMesh)child;
				String name = mesh.getName();
				String[] array = name.split("_");
				if(array.length != 2) continue;
				if(!array[1].equals(SPAWN)) continue;
				String type = array[0];
				if(type.equals(DRONE)) {
					assert(droneSpawn == null);
					droneSpawn = new SpawnSlot(mesh, orientationAngle, ms);
				} else if(type.equals(CARRIER)) {
					assert(carrierSpawn == null);
					carrierSpawn = new SpawnSlot(mesh, orientationAngle, ms);
				} else if(type.equals(HUNTER)) {
					assert(hunterSpawn == null);
					hunterSpawn = new SpawnSlot(mesh, orientationAngle, ms);
				}
			}
		}
	}
	
	public SpawnSlot getDroneSpawn() { return droneSpawn; }
	
	public SpawnSlot getCarrierSpawn() { return carrierSpawn; }
	
	public SpawnSlot getHunterSpawn() { return hunterSpawn; }
}